﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using TimKen.BussLogic;
using System.Data;
using TimKen.DataEntity;

namespace TimKen.Web.UserControls
{
    public partial class category : BaseUserControl
    {
        public PCategoryManager pcm = new PCategoryManager();
        public DataRowCollection drcPCategory;
        protected void Page_Load(object sender, EventArgs e)
        {

            //绑定产品一级分类
            if (!Page.IsPostBack)
            {
                BindTree();

                int categoryid = 0;
                if (Request.QueryString["cid"] != null)
                    categoryid = TypeParse.StrToInt(Request.QueryString["cid"], 0);
                else if (Request.QueryString["CategoryID"] != null)
                    categoryid = TypeParse.StrToInt(Request.QueryString["CategoryID"], 0);
                if (categoryid != 0)
                {
                    NineRays.WebControls.FlyTreeNode node = this.FlyTreeView1.FindByValue(categoryid.ToString());
                    if (node != null) node.Expand();

                }
            }
        }




        public void BindTree()
        {
            FlyTreeView1.Nodes.Clear();

            DataSet dsCategory = pcm.GetRoot(" LanguageID=" + LanguageID + " AND  CategoryID<>" + PCategoryManager.otherCategoryID);
            drcPCategory = dsCategory.Tables[0].Rows;

            foreach (DataRow dr in drcPCategory)
            {

                NineRays.WebControls.FlyTreeNode node = new NineRays.WebControls.FlyTreeNode();

                node.Text = dr["CategoryName"].ToString();
                node.Value = dr["CategoryID"].ToString();


                node.ToolTip = dr["CategoryName"].ToString();

                long childCount = pcm.GetListCount(" ParentID=" + dr["CategoryID"].ToString());
                if (childCount != null && childCount > 0)
                {
                    node.PopulateNodesOnDemand = true;
                }
                else
                {
                    node.PopulateNodesOnDemand = false;
                }


                this.FlyTreeView1.Nodes.Add(node);
            }
        }

        protected void FlyTreeView1_PopulateNodes(object sender, NineRays.WebControls.FlyTreeNodeEventArgs e)
        {
            if (e.Node.ChildNodes.Count <= 0)
            {
                DataSet dsChild = pcm.GetChild(int.Parse(e.Node.Value), LanguageID, 0);
                DataRowCollection drcChild = dsChild.Tables[0].Rows;
                if (drcChild == null || drcChild.Count == 0) return;


                if (drcChild != null && drcChild.Count > 0)
                {
                    foreach (DataRow drc in drcChild)
                    {
                        NineRays.WebControls.FlyTreeNode node = new NineRays.WebControls.FlyTreeNode();

                        node.Text = drc["CategoryName"].ToString();
                        node.Value = drc["CategoryID"].ToString();


                        node.ToolTip = drc["CategoryName"].ToString();

                        long childCount = pcm.GetListCount(" ParentID=" + drc["CategoryID"].ToString());
                        if (childCount != null && childCount > 0)
                        {
                            node.PopulateNodesOnDemand = true;
                        }
                        else
                        {
                            node.PopulateNodesOnDemand = false;
                        }

                        e.Node.ChildNodes.Add(node);
                    }
                }
            }
        }

        protected void FlyTreeView1_SelectedNodeChanged(object sender, NineRays.WebControls.SelectedNodeChangedEventArgs e)
        {
            e.Node.Expand();

            if (e.Node.Level == 1)
                Response.Redirect("/Product/Category.aspx?cid=" + e.Node.Value);
            else
                Response.Redirect("/Product/ChildCategory.aspx?cid=" + e.Node.Value);
        }
    }
}